﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Travel.CommonUtil.RedisUtil;
using Travel.CommonUtil.Snowflake;
using Travel.Data;
using Travel.Data.Consts;
using Travel.Data.Dtos.TravelBusiness;
using Travel.Data.Entity;
using Travel.Data.Inputs;

namespace Travel.Service.TravelBusiness.Impl
{
    public class CategoryService : ServiceBase, ICategoryService
    {
        public Results<string> AddOrUpdate(CategoryInput categoryInput)
        {
            //验重
            var count = TravelContext.TabCategories.Count(c=>c.CategoryName == categoryInput.CategoryName && !c.Deleted && c.Id != categoryInput.Id);
            if (count > 0)
            {
                return Results<string>.SuccessResult("分类重复");
            }
            //判断添加或修改
            if (categoryInput.Id > 0)
            {
                var entity = TravelContext.TabCategories.FirstOrDefault(c=>c.Id == categoryInput.Id && !c.Deleted);
                ObjectMapper.Map(categoryInput, entity);
            }
            else
            {
                var entity =  ObjectMapper.Map<TabCategory>(categoryInput);
                entity.Id = SnowIdWorker.NextId();  
                TravelContext.TabCategories.Add(entity);
            }
            TravelContext.SaveChanges();
            //删除缓存
            CacheManager.Remove(RedisKey.AllCategoryList);
            return Results<string>.SuccessResult();
        }

        /// <summary>
        /// 删除一条分类
        /// </summary>
        /// <param name="loginUserId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Results<string>> Delete(params long[] id)
        {
            if (!id.Any())
            {
                return Results<string>.FailResult("至少选中一行");
            }
            var entityList = TravelContext.TabCategories.Where(c => id.Contains(c.Id) && !c.Deleted);
            if (!entityList.Any())
            {
                return Results<string>.FailResult("未找到记录");
            }
           await  entityList.ForEachAsync(c => {
               c.Deleted = true;
               c.UpdatedUserId = LoginUserId;
            });
            TravelContext.SaveChanges();
            CacheManager.Remove(RedisKey.AllCategoryList);
            return Results<string>.SuccessResult();
        }

        /// <summary>
        /// 获取一条分类记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Results<CategoryDto> GetCategory(long id)
        {
            var entity = TravelContext.TabCategories.AsNoTracking().FirstOrDefault(c=>c.Id == id && !c.Deleted);
            if (entity == null) { 
                return Results<CategoryDto>.FailResult("未找到记录");
            }
            return Results<CategoryDto>.DataResult(ObjectMapper.Map<CategoryDto>(entity));
        }

        /// <summary>
        /// 查询所有分类
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Results<List<CategoryDto>> GetList()
        {
            var list = CacheManager.GetOrSet(RedisKey.AllCategoryList, () =>
              {
                  var tabCategories = TravelContext.TabCategories.Include("UpdatedUser").Where(c => !c.Deleted);
                  return ObjectMapper.Map<List<CategoryDto>>(tabCategories);
              }, TimeSpan.FromDays(10));
            return Results<List<CategoryDto>>.DataResult(list);

        }
    }
}
